package ${package.ServiceImpl};

<#if "true" == cfg.createExport>
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
</#if>
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${superServiceImplClassPackage};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
<#if "true" == cfg.createAdd>
import com.han.fu.api.business.model.ao.${entity}AddAo;
</#if>
<#if "true" == cfg.createUpdate>
import com.han.fu.api.business.model.ao.${entity}UpdateAo;
</#if>
<#if "true" == cfg.createPage>
import com.han.fu.api.business.model.ao.${entity}PageAo;
import com.han.fu.api.business.model.vo.${entity}Vo;
</#if>
<#if "true" == cfg.createDetail>
import com.han.fu.api.business.model.vo.${entity}InfoVo;
</#if>
<#if "true" == cfg.createExport>
import com.han.fu.api.business.model.vo.${entity}ExcelVo;
</#if>
<#if "true" == cfg.createStats>
import com.han.fu.api.business.model.ao.${entity}StatsAo;
import com.han.fu.api.business.model.vo.${entity}StatsVo;
</#if>
<#if "true" == cfg.createDelete>
import com.han.fu.api.business.model.ao.${entity}DeleteAo;
</#if>
import ${package.Service}.${table.serviceName};
import com.han.fu.common.constants.DeleteConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * ${table.comment!}接口实现类
 *
 * @author Star.zhu
 * @since 2020-03-26
 */
@Service
public class ${table.serviceImplName} extends ServiceImpl<${table.mapperName}, ${entity}> implements ${table.serviceName} {

<#if "true" == cfg.createPage>
    /**
     * ${table.comment!}分页查询
     *
     * @param ao 查询条件
     * @return Page<${entity}Vo> 分页数据
     */
    @Override
    public Page<${entity}Vo> query${entity}Page(${entity}PageAo ao) {
        return baseMapper.query${entity}Page(ao);
    }
</#if>
<#if "true" == cfg.createDetail>

    /**
     * ${table.comment!}明细查询
     *
     * @param id 明细ID
     * @return
     */
    @Override
    public ${entity}InfoVo query${entity}Detail(String id) {
        ${entity}InfoVo ${table.entityPath}InfoVo = new ${entity}InfoVo();
        ${entity} ${table.entityPath} = super.getById(id);
        if(null != ${table.entityPath}){
            BeanUtils.copyProperties(${table.entityPath}, ${table.entityPath}InfoVo);
        }
        return ${table.entityPath}InfoVo;
    }
</#if>
<#if "true" == cfg.createAdd>

    /**
     * ${table.comment!}新增数据
     *
     * @param ao 数据
     */
    @Override
    public void add${entity}(${entity}AddAo ao) {
        ${entity} entity = new ${entity}();
        BeanUtils.copyProperties(ao, entity);
        super.save(entity);
    }
</#if>
<#if "true" == cfg.createUpdate>

   /**
     * ${table.comment!}修改数据
     *
     * @param ao 数据
     */
    @Override
    public void update${entity}(${entity}UpdateAo ao) {
        ${entity} entity = new ${entity}();
        BeanUtils.copyProperties(ao, entity);
        super.updateById(entity);
    }
</#if>
<#if "true" == cfg.createDelete>

    /**
     * ${table.comment!}删除数据
     *
     * @param ao 数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete${entity}(${entity}DeleteAo ao) {
        for(String id:ao.getIds()){
            super.removeById(id);
        }
    }
</#if>
<#if "true" == cfg.createExport>

    /**
     * ${table.comment!}导出
     *
     * @param ao 分页查询条件
     * @param outputStream 输出流
     */
    @Override
    public void export${entity}(${entity}PageAo ao, ServletOutputStream outputStream) {
        ao.setPages(1);
        ao.setSize(10000);
        Page<${entity}Vo> page = baseMapper.query${entity}Page(ao);
        List<${entity}ExcelVo> list;
        if (null != page && CollectionUtils.isNotEmpty(page.getRecords())) {
            list = new ArrayList<>(page.getRecords().size());
            for (${entity}Vo vo : page.getRecords()) {
                ${entity}ExcelVo excelVo = new ${entity}ExcelVo();
                BeanUtils.copyProperties(vo, excelVo);
                list.add(excelVo);
            }
        } else {
            list = new ArrayList<>(0);
        }

        ExcelWriter excelWriter = EasyExcel.write(outputStream).build();
        excelWriter.write(list, EasyExcel.writerSheet(NumberConstants.ZERO, "${table.comment!}").head(${entity}ExcelVo.class).build());
        excelWriter.finish();
    }
</#if>
<#if "true" == cfg.createStats>

    /**
     * ${table.comment!}统计查询
     *
     * @param ao 查询条件
     * @return List<${entity}StatsVo> 统计数据
     */
    @Override
    public List<${entity}StatsVo> query${entity}Stats(${entity}StatsAo ao) {
        return baseMapper.query${entity}Stats(ao);
    }
</#if>

}